# -*- coding: utf-8 -*-
import scrapy
import re
from PhoneSpider.items import PhonespiderItem
from pandas import DataFrame
import pandas as pd
import json
class phonespider(scrapy.spiders.Spider):
    name = 'phoneSpider'
    allowed_domain = {'zol.com.cn'}
    start_urls = {
        'http://detail.zol.com.cn/cell_phone_index/subcate57_0_list_2500_s5394_1_1_0_1.html'
    }
    sheet = 1
    data = list()
    count = 0
    phone = {}
    def parse(self,response):
        #获取页码数
        for sel in response.xpath('//*[@class = "pro-intro"]'):
            self.phone['name'] = sel.xpath('h3/a/text()').extract()[0].encode('utf-8')
            #print self.phone['name']
            for param in sel.xpath('ul[@class="param clearfix"]/li'):
                pn = param.xpath('span/text()').extract()[0].encode('utf-8').split('：')[0]
                self.phone[pn]= param.xpath('@title').extract()
            self.data.append(DataFrame(self.phone))

        if self.sheet<62:
            self.sheet = self.sheet+1
            url = 'http://detail.zol.com.cn/cell_phone_index/subcate57_0_list_2500_s5394_1_1_0_%d.html' % self.sheet
            yield scrapy.Request(url, callback=self.parse)
        else:
            pd.concat(self.data).set_index('name').to_csv('./res/phoneall.csv')


